home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / 0574.ZIP / FGETC.ASM < prev    next >
Assembly Source File  |  1986-12-01  |  2KB  |  100 lines

  1. include compiler.inc
  2.  
  3.     ttl    FGETC, 1.06, 11-20-86, cr
  4.  
  5. ;single character stream input
  6.  
  7.     dseg
  8.  
  9.     cseg
  10.  
  11.     xtfs    <read,$strhand>
  12.  
  13. MODE    equ    byte ptr 2[si]
  14. CRFLG    equ    byte ptr 3[si]
  15.  
  16.     alias fgetc,getc
  17.  
  18.     procdef    fgetc, <<streamp, ptr>>
  19.     locs    <<bufp,word>>
  20.     pushreg
  21.     pushds
  22.  
  23.     xor    di,di            ;blank local buffer
  24.  
  25.     callit    $strhand <<streamp,ptr>>
  26.     inc    ax
  27.     jnz    tmp1
  28.     jmp    reteof            ;bad pointer...
  29. tmp1:
  30.     dec    ax            ;ax holds handle
  31.     ldptr    si, streamp
  32.  
  33.     test    bl,1            ;bl holds MODE
  34.     jz    seterr            ;not open for reading
  35.     test    bl,18h
  36.     jnz    reteof            ;already set
  37.     inc    cx            ;test for UGC
  38.     jz    lfloop            ;no char waiting
  39.     dec    cx
  40.     mov    di,cx            ;had one in UGC
  41.     mov    word ptr [si],-1
  42.     jmp    short gotbyt
  43. ;
  44. lfloop:
  45.     push    ax            ;save handle
  46.     push    ds
  47.     mov    bx,1            ;get one byte
  48.     lea    cx,bufp            ;address of buffer
  49.     callit    read,<<bx,reg>,<cx,preg,ss>,<ax,reg>>
  50.     mov    di,bufp
  51.     and    di,0ffh
  52.  
  53.     pop    ds
  54.     pop    dx            ;recover handle in DX!!
  55.     or    ax,ax
  56.     jz    seteof            ;nothing returned - set EOF
  57.     inc    ax
  58.     jnz    gotbyt
  59. seterr:
  60.     or    MODE,10h        ;set error flag
  61.     jmp    short    seteof
  62. gotbyt:
  63.     test    MODE,4            ;have a byte in DI
  64.     jnz    getval            ;not in raw mode
  65.     mov    ax,di
  66.     cmp    al,0dh            ;was it CR
  67.     jnz    notcr
  68.     mov    CRFLG,1            ;yes, set flag
  69.     mov    ax,0ah
  70.     jmp    short fini        ;and return LF
  71. ;
  72. notcr:    cmp    al,0ah            ;was it LF
  73.     jnz    notlf
  74.     test    CRFLG,1            ;CR flag set?
  75.     jz    getval            ;no
  76.     mov    CRFLG,0            ;yes, clear it
  77.     mov    ax,dx            ; restore handle
  78.     jmp    lfloop            ;and get next byte
  79. ;
  80. notlf:    cmp    al,1ah            ;was it CPMEOF
  81.     jnz    notef
  82. seteof:
  83.     or    MODE,08h        ;yes, set EOF
  84. reteof:
  85.     mov    ax,-1
  86.     jmp    short    fini
  87. ;
  88. notef:
  89.     mov    CRFLG,0            ;clear CR flag
  90. getval:
  91.     mov    ax,di            ;and return byte
  92.     xor    ah,ah            ;as unsigned
  93. fini:
  94.     pret
  95.  
  96.     pend    fgetc
  97.  
  98.     finish
  99. 
  100.